package com.hq.fxh.CustomerView;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.hq.fxh.R;
import com.hq.fxh.Utils.LogUtils;
import com.hq.fxh.Utils.Tool;

import java.util.Timer;
import java.util.TimerTask;


/**
 * Created by Administrator on 2017/2/16.
 */
public class PopupForEditZhiDing extends PopupWindow implements View.OnClickListener {

    protected View rootView;
    protected TextView tvPlateEditorBack;
    protected TextView textToolBarTitle;
    protected TextView tvPlateEditorPublish;
    protected TextView tvPopupZhiDingHours;
    protected TextView tvPopupZhiDingDays;
    protected TextView tvPopupZhiDingMonths;
    protected EditText editPopupZhiDingTime;
    protected TextView tvPopupZhiDingExpense;
    protected TextView tvPopupZhiDingMyMoney;
    protected TextView tvPopupZhiDingTimeCell;
    private Activity context;
    private View popView;
    ZhiDingCallBack commentCallBack;

    public PopupForEditZhiDing(Activity context, ZhiDingCallBack commentCallBack) {
        super(context);
        this.context = context;
        this.commentCallBack = commentCallBack;
        popView = LayoutInflater.from(context).inflate(R.layout.popup_edit_zhiding, null);
        initView(popView);
        initData();
        initListener();

    }


    public void openKeyboard() {

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);

            }
        }, 300);
    }

    public void initData() {

        //设置PopupWindow的View
        this.setContentView(popView);
        //设置PopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置PopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置PopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setAnimationStyle(R.style.dialog_anim_style);
        this.setOutsideTouchable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xffffffff);
    /*    openKeyboard();*/

        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                Tool.setAlpha(context, 1f);
                editPopupZhiDingTime.setText(" ");

            }
        });


    }


    private void initView(View rootView) {
        tvPlateEditorBack = (TextView) rootView.findViewById(R.id.tv_PlateEditor_Back);
        tvPlateEditorBack.setOnClickListener(PopupForEditZhiDing.this);
        textToolBarTitle = (TextView) rootView.findViewById(R.id.textToolBar_Title);
        tvPlateEditorPublish = (TextView) rootView.findViewById(R.id.tv_PlateEditor_Publish);
        tvPlateEditorPublish.setOnClickListener(PopupForEditZhiDing.this);
        tvPopupZhiDingHours = (TextView) rootView.findViewById(R.id.tv_PopupZhiDing_Hours);
        tvPopupZhiDingHours.setOnClickListener(PopupForEditZhiDing.this);
        tvPopupZhiDingDays = (TextView) rootView.findViewById(R.id.tv_PopupZhiDing_Days);
        tvPopupZhiDingDays.setOnClickListener(PopupForEditZhiDing.this);
        tvPopupZhiDingMonths = (TextView) rootView.findViewById(R.id.tv_PopupZhiDing_Months);
        tvPopupZhiDingMonths.setOnClickListener(PopupForEditZhiDing.this);
        editPopupZhiDingTime = (EditText) rootView.findViewById(R.id.edit_PopupZhiDing_Time);
        tvPopupZhiDingExpense = (TextView) rootView.findViewById(R.id.tv_PopupZhiDing_Expense);
        tvPopupZhiDingMyMoney = (TextView) rootView.findViewById(R.id.tv_PopupZhiDing_MyMoney);
        tvPopupZhiDingTimeCell = (TextView) rootView.findViewById(R.id.tv_PopupZhiDing_TimeCell);
        textToolBarTitle.setText("置顶方式");
        tvPlateEditorPublish.setText("确定");
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.tv_PlateEditor_Back) {
            dismiss();

        } else if (view.getId() == R.id.tv_PopupZhiDing_Hours) {
            commentCallBack.call("时");
            tvPopupZhiDingTimeCell.setText("时");
            changeText(tvPopupZhiDingHours);


        } else if (view.getId() == R.id.tv_PopupZhiDing_Days) {
            commentCallBack.call("天");
            tvPopupZhiDingTimeCell.setText("天");
            changeText(tvPopupZhiDingDays);


        } else if (view.getId() == R.id.tv_PopupZhiDing_Months) {
            commentCallBack.call("月");
            tvPopupZhiDingTimeCell.setText("月");
            changeText(tvPopupZhiDingMonths);

        } else if (view.getId() == R.id.tv_PlateEditor_Publish) {
            if (Tool.editIsEmpty(editPopupZhiDingTime)||editPopupZhiDingTime.getText().equals("0")) {
                LogUtils.startToash(context, "请输入置顶天数");
            } else {
                String str = String.valueOf(editPopupZhiDingTime.getText());

                commentCallBack.Sure( Tool.getIntFromString(str));
            }
            dismiss();
        }
    }

    private void changeText(TextView textView) {
        tvPopupZhiDingHours.setBackgroundResource(R.drawable.style_edit);
        tvPopupZhiDingHours.setTextColor(context.getResources().getColor(R.color.textblack));
        tvPopupZhiDingDays.setTextColor(context.getResources().getColor(R.color.textblack));
        tvPopupZhiDingDays.setBackgroundResource(R.drawable.style_edit);
        tvPopupZhiDingMonths.setTextColor(context.getResources().getColor(R.color.textblack));
        tvPopupZhiDingMonths.setBackgroundResource(R.drawable.style_edit);

        textView.setTextColor(context.getResources().getColor(R.color.white));
        textView.setBackgroundResource(R.drawable.styleredbtn);


    }

    private void initListener() {
        editPopupZhiDingTime.setFocusable(true);
        editPopupZhiDingTime.setFocusableInTouchMode(true);
        editPopupZhiDingTime.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }


    public interface ZhiDingCallBack {
        void call(String comment);


        void Sure(int money);

    }


}
